from celery_tasks.main import celery_app
from celery_tasks.yuntongxun.sms import CCP
import logging

logger = logging.getLogger('django')

# 定义任务函数
# @celery_app.task(name='test_demo')
# def celery_tasks(a, b):
#     print('a:%s\nb:%s' % (a, b))

# 不依赖于项目,定义常量
SMS_CODE_TEMP_ID = 1


@celery_app.task(name='send_sms_code')
def send_sms_code(mobile, expire, sms_code):
    # 异步发送短信
    try:
        # 为了不依赖于项目,将云通讯报拷进来
        res = CCP().send_template_sms(mobile, [sms_code, expire], SMS_CODE_TEMP_ID)

    except Exception as e:
        logger.error('发送短信异常: [mobile: %s] [sms_code: %s]' % (mobile, sms_code))
    else:
        if res != 0:
            # 发送短信失败
            logger.error('发送短信失败: [mobile: %s] [sms_code: %s]' % (mobile, sms_code))
        else:
            # 发送短信成功
            logging.info('发送短信成功: [mobile: %s] [sms_code: %s]' % (mobile, sms_code))
